﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using PdfCreative.Pdf.Writer;

namespace PdfCreative.Pdf.Graphics
{
    /// <summary>
    /// The PatternBrush class represents a brush that uses a pattern to fill surfaces. It is the abstract base class of all
    /// pattern and gradient brushes.
    /// </summary>
    public abstract class PatternBrush : Brush, IResourceWriter
    {
        // IResourceWriter implementation
        // ==============================
        #region IResourceWriter Members
        /// <summary>
        /// True if this resource is a custom resource. False otherwise.
        /// </summary>
        public bool IsCustomResource { get { return true; } }

        /// <summary>
        /// Writes the resource for this stock object.
        /// </summary>
        /// <param name="graphics">The graphics object to use as a context</param>
        /// <param name="writer">The writer to write to</param>
        /// <param name="resourceReference">The resource reference that has been assigned to this stock object.</param>
        /// <param name="scope">The scope of this resource instance.</param>
        public abstract void WriteResource(Graphics graphics, PdfWriter writer, PdfResourceReference resourceReference, ScopedResource scope);

        /// <summary>
        /// Retrieves the scope that this resource is valid on.
        /// </summary>
        public abstract ResourceScope Scope { get; }
        #endregion



        // Brush overrides
        // ===============
        #region Brush overrides
        /// <summary>
        /// Cloning a PatternBrush is not supported.
        /// </summary>
        /// <returns>(not applicable)</returns>
        /// <exception cref="NotImplementedException">Cloning a PatternBrush is not supported</exception>
        public override Brush Clone()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Indicates that the brush requires a resource.
        /// </summary>
        internal protected override bool RequiresResource
        {
            get { return true; }
        }

        /// <summary>
        /// Writes the changes between this brush and the other brush.
        /// </summary>
        /// <param name="writer">The writer to use</param>
        /// <param name="otherBrush">The other brush</param>
        protected internal override void WriteChanges(PdfWriter writer, Brush otherBrush)
        {
            System.Diagnostics.Debug.Fail("The PatternBrush is a resource brush and needs the WriteResource method to be invoked.");
        }

        /// <summary>
        /// Writes a command for this pattern brush to the specified writer.
        /// </summary>
        /// <param name="writer">The writer to write to</param>
        /// <param name="resource">The resource to write</param>
        protected internal override void WriteResource(PdfWriter writer, PdfResourceReference resource)
        {
            writer.WriteRawContent(String.Format("/Pattern cs /{0} scn\r\n", resource.Identifier));
        }

        #endregion



        // Protected implementation
        // ========================
        #region Protected implementation
        /// <summary>
        /// Retrieves the PDF name of the colour space for the given colour.
        /// </summary>
        /// <param name="colour">The colour.</param>
        /// <returns>The PDF name of the colour space.</returns>
        protected string GetPdfColourSpace(Colour colour)
        {
            if (null == colour)
                throw new ArgumentNullException("colour");

            string name = null;

            switch (colour.ColourSpace)
            {
                case ColourSpace.DeviceCMYK:
                    name = "/DeviceCMYK";
                    break;

                case ColourSpace.DeviceRGB:
                    name = "/DeviceRGB";
                    break;
            }

            return name;
        } // GetPdfColourSpace()

        /// <summary>
        /// Retrieves an array with the specified colour's components, formatted as a string.
        /// </summary>
        /// <param name="colour">The colour</param>
        /// <returns>The string</returns>
        protected string WriteColourArray(Colour colour)
        {
            if (null == colour)
                throw new ArgumentNullException("colour");

            StringBuilder sb = new StringBuilder();
            sb.Append("[");

            foreach (byte component in colour.GetComponents())
            {
                sb.Append(String.Format(" {0:F4}", component/255f));
            }

            sb.Append("]");
            return sb.ToString();
        }
        #endregion

    } // PatternBrush
}
